package eu.silentspycamera.menu;

import eu.silentspycamera.R;
import eu.silentspycamera.R.id;
import eu.silentspycamera.R.layout;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class Preferences extends Activity {

	
	SharedPreferences preferences;
	int opacity; //This is the current opacity
	boolean opacityChanged = false;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.preferences);
        
        preferences = getSharedPreferences(PreferenceValues.PREFERENCES_FILE, MODE_PRIVATE);
        
        initializeSeekBar();
        initializeToastCheckBox();
        initializeToastEditText();
    }
    
    @Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}
    
    private void initializeSeekBar() {
        SeekBar seekBar = (SeekBar)findViewById(R.id.opacitySeekBar);
        seekBar.setProgress(preferences.getInt(PreferenceValues.OPACITY_KEY, PreferenceValues.STANDARD_OPACITY));
        seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
        	
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				opacityChanged = true;
				setResult(RESULT_OK);
		    	SharedPreferences.Editor editor = preferences.edit();
		    	editor.putInt(PreferenceValues.OPACITY_KEY, seekBar.getProgress());
		    	editor.commit();	
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) { 
				// No use 
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				// No use
			}
		});
    }
    
    private void initializeToastCheckBox() {
    	CheckBox chb = (CheckBox)findViewById(R.id.enableToast);
    	chb.setChecked(preferences.getBoolean(PreferenceValues.TOAST_ENABLED_KEY, PreferenceValues.STANDARD_TOAST_ENABLED));
    	chb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				setResult(RESULT_OK);
		    	SharedPreferences.Editor editor = preferences.edit();
		    	editor.putBoolean(PreferenceValues.TOAST_ENABLED_KEY, isChecked);
		    	editor.commit();
			}
		});
    }
    
    private void initializeToastEditText() {
    	EditText edt = (EditText)findViewById(R.id.toastText);
    	edt.setText(preferences.getString(PreferenceValues.TOAST_TEXT_KEY, PreferenceValues.STANDARD_TOAST_TEXT));
    	edt.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
		    	SharedPreferences.Editor editor = preferences.edit();
		    	editor.putString(PreferenceValues.TOAST_TEXT_KEY, s.toString());
		    	editor.commit();
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				// No use
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// No use
			}
		});
    }
    
    @Override
    protected void onStop() {
    	super.onStop();	
    }
}